<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
defined ('IN_AGORA') or die;
 
class TaskController extends AgoraCommonController {	
	
	function __construct() {
		parent::__construct();
		if (method_exists($this,'loadModel')) {
			$this->loadModel();
		}

	}

	function setPagination($total) {
		$app = &JFactory::getApplication();
		$limit		= $app->getUserStateFromRequest( 'global.list.limit',	'limit',		$app->getCfg( 'list_limit' ),	'int' );
		$limitstart	= $app->getUserStateFromRequest( 'com_agora.limitstart','limitstart',	0,								'int' );
		
		jimport('joomla.html.pagination');
		$this->pagination = new JPagination( $total, $limitstart, $limit );
		if ($limit > 0) {
			$this->pagination->page = $limitstart / $limit + 1;
			$this->pagination->per_page = $limit;
		} else {
			$this->pagination->page = 1;
			$this->pagination->per_page = $total;
		}

		$this->view->assign('pagination',$this->pagination);
	}

	function _default() {
		$this->loadView('AdminView','adminview.php');
		$this->view->template = 'maintenance/mergeusers';
	}
		
	function choose() {
		$this->loadView('AdminView','adminview.php');
		$sort_dir = Agora::getVar('filter_order_Dir','ASC');
		$sort_field = Agora::getVar('filter_order');

		$users_model = &Model::getInstance('UserModel');
		$users_model->sync();

		// sanity checks
		if (strcasecmp($sort_dir,'asc') != 0 && strcasecmp($sort_dir,'desc')) {
			$sort_dir = 'asc';
		}

		if (!in_array($sort_field,array('last_visit','num_posts','username','registered','email'))) {
			$sort_field = null;
		}

		if ($sort_field) {
			$users_model->setOrder('ORDER BY '.$sort_field.' '.$sort_dir);
		}


		$filter_strict = Agora::getPostVar('filter_strict');
		
		$addtype = Agora::getVar('tp');

		$filter = Agora::getPostVar('filter');
		if (!is_array($filter)) {
			$filter = array();
		}

		if (isset($filter['username'])) {
			if ($filter_strict) {
				$users_model->setFilter('username',$filter['username'], '=', 'j_u.');
			} else {
				$users_model->setFilter('username','%'.$filter['username'].'%','LIKE', 'j_u.');
			}
		} elseif (isset($filter['email'])) {
			if ($filter_strict) {
				$users_model->setFilter('email',$filter['email'], '=', 'j_u.');
			} else {
				$users_model->setFilter('email','%'.$filter['email'].'%','LIKE', 'j_u.');
			}
		}

		$this->setPagination($users_model->loadAllCount());
		$users = $users_model->loadAll('',$this->pagination->per_page,$this->pagination->page);

		$this->view->assign('addtype',$addtype);
		$this->view->assign('filter',$filter);
		$this->view->assign('filter_strict',$filter_strict);
		$this->view->assign('sort_dir',$sort_dir);
		$this->view->assign('sort_field',$sort_field);


		$group_model = & Model::getInstance('GroupModel');
		$groups = $group_model->loadAll();
		$groups = parseTree($groups,0,0,'id','parent_id', true);
		$this->view->assign('groups',$groups);
		$this->view->assign('users',$users);
		$this->view->template = 'maintenance/mergechoose';
	}

	function filter() {
		$filter = Agora::getVar('filter');
		if (!$filter) {
			$filter = array();
		}

		$args = array();
		foreach ($filter as $f_name=>$f) {
			if ($f)
			$args[] = 'filter['.$f_name.']='.$f;
		}

		$strict = Agora::getVar('filter_strict',0);
		$args[] = 'filter_strict='.$strict;
		
		$users_model = &Model::getInstance('UserModel');
		$this->setPagination($users_model->loadAllCount());

		call_user_func_array(array($this,'redirect'), $args);
	}

	function sort() {
		$this->redirect('filter_order','filter_order_Dir');
	}
	
	function merge() {

		$this->db = & JFactory::getDBO();
		$lang = & JFactory::getLanguage();
		$lang->load('com_agora',JPATH_ROOT);

		echo '<h2>'.Agora::lang('Merge users').'</h2>';
		
		$from_id = Agora::getPostVar('from_id',0);
		$from_uname = Agora::getPostVar('from_uname','');
		$to_id = Agora::getPostVar('to_id',0);
		$to_uname = Agora::getPostVar('to_uname','');

		// chek input values
		if (!(int)$to_id || !$from_uname || !$to_uname) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('Incomplete user data') . '</span>';
			return false;
		}
		
		// bans
		$this->db->setQuery('UPDATE #__agora_bans SET `username` = '.$this->db->Quote($to_uname).' WHERE `username` = '.$this->db->Quote($from_uname));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('bans') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;			
		}

		// messages
		$this->db->setQuery('UPDATE #__agora_messages SET `sender` = '.$this->db->Quote($to_uname).',`sender_id` = '.$this->db->Quote($to_id).'  WHERE `sender_id` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('messages') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;			
		}
		$this->db->setQuery('UPDATE #__agora_messages SET `owner` = '.$this->db->Quote($to_id).' WHERE `owner` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('messages') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;			
		}

		// forums
		$this->db->setQuery('UPDATE #__agora_forums SET `last_poster` = '.$this->db->Quote($to_uname).' WHERE `last_poster` = '.$this->db->Quote($from_uname));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('forums') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;			
		}
		
		// posts
		$this->db->setQuery('UPDATE IGNORE #__agora_posts SET `poster` = '.$this->db->Quote($to_uname).',`poster_id` = '.$this->db->Quote($to_id).'  WHERE `poster_id` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('posts') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;			
		}
		$this->db->setQuery('UPDATE #__agora_posts SET `edited_by` = '.$this->db->Quote($to_uname).' WHERE `edited_by` = '.$this->db->Quote($from_uname));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('posts') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;			
		}

		// topics
		$this->db->setQuery('UPDATE #__agora_topics SET `last_poster` = '.$this->db->Quote($to_uname).' WHERE `last_poster` = '.$this->db->Quote($from_uname));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('topics') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;			
		}
		$this->db->setQuery('UPDATE #__agora_topics SET `poster` = '.$this->db->Quote($to_uname).' WHERE `poster` = '.$this->db->Quote($from_uname));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('topics') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;			
		}

		// ratings
		$this->db->setQuery('UPDATE #__agora_ratings SET `user_id` = '.$this->db->Quote($to_id).' WHERE `user_id` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('ratings') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}

		// read posts
		$this->db->setQuery('UPDATE IGNORE #__agora_read_posts SET `user_id` = '.$this->db->Quote($to_id).' WHERE `user_id` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('read posts') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}

		// subscriptions
		$this->db->setQuery('UPDATE IGNORE #__agora_subscriptions SET `user_id` = '.$this->db->Quote($to_id).' WHERE `user_id` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('subscriptions') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}

		// user warning
		$this->db->setQuery('UPDATE #__agora_user_warning SET `user_id` = '.$this->db->Quote($to_id).' WHERE `user_id` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('user warning') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}

		// reports
		$this->db->setQuery('UPDATE #__agora_reports SET `reported_by` = '.$this->db->Quote($to_id).' WHERE `reported_by` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('reports') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}
		$this->db->setQuery('UPDATE #__agora_reports SET `zapped_by` = '.$this->db->Quote($to_id).' WHERE `zapped_by` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('reports') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}
		
		// reputation
		$this->db->setQuery('UPDATE #__agora_reputation SET `user_id` = '.$this->db->Quote($to_id).' WHERE `user_id` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('reputation') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}
		$this->db->setQuery('UPDATE #__agora_reputation SET `from_user_id` = '.$this->db->Quote($to_id).' WHERE `from_user_id` = '.$this->db->Quote($from_id));
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('reputation') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}
		
		// recount user posts
		$this->db->setQuery('UPDATE #__agora_users SET num_posts = 
							(select count(*) from #__agora_posts 
							WHERE poster_id=#__agora_users.id)');
		if (!$this->db->query()) {
			echo '<span style="color:red;">' . Agora::lang('Error') . ' (' . Agora::lang('recount osts') . ') - ' . $this->db->_errorMsg . '</span>';
			return false;
		}
		
		echo Agora::lang('Done');
		
	}

}
?>
